package com.cooper.android.comm;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public abstract class DownReceiverChain {

	public DownReceiverChain(DownloadManager manager) {
		super();
		this.manager = manager;
	}

	public DownReceiverChain() {
		super();
	}

	private DownReceiverChain downReceiverChain = null;

	public void addReceiver(DownReceiverChain downReceiverChain) {
		downReceiverChain.manager = manager;
		this.downReceiverChain = downReceiverChain;
	}

	protected abstract boolean onReceive(Context context, Intent intent);

	public void receive(Context context, Intent intent) {
		if (!onReceive(context, intent)) {
			sendReceive(context, intent);
		}
	}

	private void sendReceive(Context context, Intent intent) {
		if (downReceiverChain != null) {
			downReceiverChain.receive(context, intent);
		}
	}

	private DownloadManager manager;

	protected DownloadManager getDownloadManager(Context context) {
		if (manager == null) {
			manager = (DownloadManager) context
					.getSystemService(Activity.DOWNLOAD_SERVICE);
		}
		return manager;
	}

}
